package com.thingsgrid.application.conf;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;


public class CopyRightUtils {

//	public static void main(String[] args) throws Exception {
//
//		String dir = "C:\\Users\\TRANS\\Desktop\\0908\\工单微服务\\微服务";
//		//System.out.println("输入路径为："+ num);
//		File file = new File(dir);
//		addCopyright4Directory(file);
//	}

	public static void addCopyright4Directory(File file) throws Exception {
		File[] files = file.listFiles();
		if (files == null || files.length == 0){
			return;
		}

		for (File f : files) {
			if (f.isFile()) {
				addCopyright4File(f);
				System.out.println("文件  ---> " + f.getName());
			} else {
				System.out.println("目录 :   " + f.getName());
				addCopyright4Directory(f);
			}
		}
	}

	public static void addCopyright4File(File file) throws Exception {
		String fileName = file.getName();
		boolean isJava = fileName.endsWith(".java");
		if (!isJava) {
			System.err.println("This file is not java source file, filaName=" + fileName);
			return;
		}

		if (isJava) {
			// 版权字符串
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			String content = "";
			//读取一行，一定要加上“换行符”,Windows下可以直接用“\n”
			String lineSeperator ="\n";
			//lineSeperator = System.getProperty("line.separator")

			boolean isPackage =false;



			while ((line = br.readLine()) != null  ) {
				if(line.startsWith("package com.")) isPackage = true;
				if(isPackage){
					content += line + lineSeperator;
				}
			}
			br.close();
			//把拼接后的字符串写回去
			FileWriter fileWriter = new FileWriter(file);
			fileWriter.write(content);
			fileWriter.close();
		}

	}
}
